package com.bw.liuyushan.mykotlin_firstmvptest.MVVM

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.liuyushan.mykotlin_firstmvptest.MVP.Entity.Data
import kotlinx.android.synthetic.main.activity_m_v_m_p.*
import com.bw.liuyushan.mykotlin_firstmvptest.R
import kotlinx.android.synthetic.main.activity_m_v_m_p.*

/**
 *MyKotlin_FirstMVPTest
 *file name is : MVVMActivity
 *created by Ender on 2021/9/16 10:32.
 *author : 刘雨杉
 *Describe:练习的MVVM框架半成品
 */
class MVVMActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //取
        setContentView(R.layout.activity_m_v_m_p)

        //相当于new
        val viewModel = ViewModelProvider(this).get(MVVMViewModel::class.java)


        mvvm_rv.layoutManager = LinearLayoutManager(this)
        var data = mutableListOf<Data>()
        val foodAdapter = FoodAdapter(R.layout.food_layout, data)

        mvvm_rv.adapter = foodAdapter

        //请求数据
        viewModel.getFood()


        viewModel.data.observe(this){
            Log.i("data",it.size.toString())
            data.addAll(it)
            foodAdapter.notifyDataSetChanged()
        }


        load.setOnClickListener{

        }









//

//        add.setOnClickListener{
//            viewModel.add()
//        }
//
//        viewModel.sum.observe(this){
//            tv_num.text = viewModel.sum.value.toString()
//        }

        //观察
//        viewModel.count.observe(this){
//            tv_num.text = viewModel.count.value.toString()
//        }








//        add.setOnClickListener {
//           viewModel.add()
//            tv_num.text = viewModel.count.toString()
//        }
//        tv_num.text = viewModel.count.toString()
    }

}